package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.BlnoContactErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.TblisustampMainErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.vo.blnocontactvo.BlnoContactBaseVO;
import com.cmc.cloud.cmclink.doc.vo.blnocontactvo.BlnoContactCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnocontactvo.BlnoContactPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnocontactvo.BlnoContactUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import java.util.*;

import com.cmc.cloud.cmclink.doc.service.BlnoContactService;
import com.cmc.cloud.cmclink.doc.entity.BlnoContactDO;
import com.cmc.cloud.cmclink.doc.mapper.BlnoContactMapper;


/**
 * 目的港代理联系方式维护 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class BlnoContactServiceImpl implements BlnoContactService {

    @Resource
    private BlnoContactMapper blnoContactMapper;

    @Override
    @Lock4j(name = "blnoContactUnique", keys = {"#createReqVO.getLaneCode", "#createReqVO.getPortCode"})
    public void createBlnoContact(BlnoContactCreateReqVO createReqVO) {
        // 插入
        validateUnique(createReqVO, null);
        BlnoContactDO blnoContact = BeanUtil.copyProperties(createReqVO, BlnoContactDO.class);
        blnoContactMapper.insert(blnoContact);
    }

    @Override
    @Lock4j(name = "blnoContactUnique", keys = {"#updateReqVO.getLaneCode", "#updateReqVO.getPortCode"})
    public void updateBlnoContact(BlnoContactUpdateReqVO updateReqVO) {
        // 校验存在
        validateBlnoContactExists(updateReqVO.getId());
        validateUnique(updateReqVO, updateReqVO.getId());
        // 更新
        BlnoContactDO updateObj = BeanUtil.copyProperties(updateReqVO, BlnoContactDO.class);
        blnoContactMapper.updateById(updateObj);
    }

    @Override
    public void deleteBlnoContact(Long id) {
        // 校验存在
        validateBlnoContactExists(id);
        // 删除
        blnoContactMapper.deleteById(id);
    }

    private void validateBlnoContactExists(Long id) {
        if (blnoContactMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(BlnoContactErrorCodeConstants.BLNO_CONTACT_NOT_EXISTS);
        }
    }

    private void validateUnique(BlnoContactBaseVO vo, Long id) {
        LambdaQueryWrapperX<BlnoContactDO> queryWrapperX = new LambdaQueryWrapperX<BlnoContactDO>()
                .eq(BlnoContactDO::getLaneCode, vo.getLaneCode())
                .eq(BlnoContactDO::getPortCode, vo.getPortCode());
        if (id != null) {
            queryWrapperX.ne(BlnoContactDO::getId, id);
        }
        if (blnoContactMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(TblisustampMainErrorCodeConstants.TBLISUSTAMP_EXISTED);
        }
    }

    @Override
    public BlnoContactDO getBlnoContact(Long id) {
        return blnoContactMapper.selectById(id);
    }

    @Override
    public List<BlnoContactDO> getBlnoContactList(Collection<Long> ids) {
        return blnoContactMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlnoContactDO> getBlnoContactPage(BlnoContactPageReqVO reqVO) {

        LambdaQueryWrapperX<BlnoContactDO> queryWrapperX = new LambdaQueryWrapperX<BlnoContactDO>()
                .likeIfPresent(BlnoContactDO::getPortCode, reqVO.getPortCode());
        return blnoContactMapper.selectPage(reqVO, queryWrapperX);
    }
}
